app.controller("z-department-c", [ '$scope','$rootScope','$z-http','$sce','z-department-s','z-corporation-s','z-attendance-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,CorporationService,AttendanceService,MenuService,$filter) {
	$scope.viewModel = {};
	$scope.viewModel.perPage = "10";
	$scope.viewModel.rowCount = "1";
	$scope.editModel = {};
	$scope.editModel.department={};
	$scope.editModel.parentDepartment={};
	$scope.editModel.title="";
	$scope.viewModel.departmentName="";
	//下拉多选框长度
	$scope.multiSelectMaxHeight=300;

	// 定义生效时间框内的值，用于检索
	$scope.editModel.changeEffectiveDate = new Date();
	// 获取当天日期
	$scope.viewModel.effectiveDate = new Date();
	//查询页面名称
	MenuService.getMenuByUrl({url:'department'},
	function(data){	
		$scope.viewModel.menuList=data;
	},function(error) {});
	
	//公司
	$('#dCorporation').multiselect({
		includeSelectAllOption: true,
		enableFiltering: true,
		delimiterText: '; ',
        maxHeight: $scope.multiSelectMaxHeight
    });
	
	//加载考勤状态数据
	$scope.viewModel.bindSelectData = function(){
		//查询公司
		CorporationService.getCorporationList({
			fromDate:$scope.viewModel.changeEffectiveDate,
		},
		function(data){
			var site = [];  
	        $.each(data, function(index, values) {  
	            site.push({  
	                label : "("+values.corporationCode+")"+values.corporationName,  
	                value : values.corporationCode  
	            });  
	        });  
	        $('#dCorporation').multiselect('dataprovider',site);
		},function(error) {
			
		});
	};
	$scope.viewModel.bindSelectData();
	
	//加载数据
//	$scope.viewModel.ListBindData = function(){
//		$scope.viewModel.isLoading=true;
//		//获取公司名列表数据
//		CorporationService.getCorporationList({
//			
//		},
//		function(data){
//			$scope.viewModel.corDataList = data;
//			$scope.viewModel.isLoading=false;
//		},function(error) {
//			$rootScope.alertError(error.fault.message);
//			$scope.viewModel.isLoading=false;
//		});
//		
//	};
//	$scope.viewModel.ListBindData();
	//加载
	$scope.viewModel.load=function(){
		$scope.viewModel.show=true;
	};
	//关闭加载
	$scope.viewModel.unLoad=function(){
		$scope.viewModel.show=false;
	};
	
	//新增
	$scope.viewModel.add = function(){
		$scope.viewModel.load();
		$scope.editModel.title="新增部门";
		$scope.editModel.type = true;
		$scope.editModel.department={};
		//验证不能为空
		$scope.editForm.departmentName.$dirty=false;
		$scope.editForm.departmentCode.$dirty=false;
		$scope.editForm.corporation.$dirty=false;
	};
	//修正
	$scope.viewModel.update=function(department){
		$scope.viewModel.load();
		$scope.editModel.title="修改部门";
		$scope.editModel.type = false;
		$scope.editModel.department=angular.copy(department);
		if($scope.editModel.department.parentDepartmentCode!=null)
		$scope.editModel.parentDepartment.upDepartment = "("+$scope.editModel.department.parentDepartmentCode+")" + $scope.editModel.department.parentDepartmentName;
		$scope.editModel.department.orgLevel = department.orgLevel;
	};

	//验证
	$scope.viewModel.requiredForm= function() {
        if (!$scope.editForm.$valid) {
        	if(!$scope.editForm.departmentName.$dirty){
        		$scope.editForm.departmentName.$dirty=true;
        	}
        	if(!$scope.editForm.departmentCode.$dirty){
        		$scope.editForm.departmentCode.$dirty=true;
        	}
        	if(!$scope.editForm.corporation.$dirty){
        		$scope.editForm.corporation.$dirty=true;
        	}
            return true;
        }else{
        	if($scope.editModel.department.corporationId == null){
        		$rootScope.alertWarning("未选择公司！");
        		return true;
        	}
        	$scope.editForm.corporation.$invalid=false;
        	return false;
        }
    };
	
	//保存
	$scope.viewModel.save = function(){
		if($scope.viewModel.requiredForm()){
			return;
		}
		$scope.editModel.department.fromDate = $scope.editModel.changeEffectiveDate;
		$scope.viewModel.isLoading = true;
		if($scope.editModel.type){
			Service.saveDepartment($scope.editModel.department,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("新增成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
		}else{
			Service.updateDepartment($scope.editModel.department,
					function(data){
						$scope.viewModel.unLoad();
						$scope.viewModel.bindData();
						$rootScope.alertInfo("修改成功。");
						$scope.viewModel.isLoading = false;
					},function(error) {
						$rootScope.alertError(error.fault.message);
						$scope.viewModel.isLoading = false;
					});
		}
	};
	
	//删除
	$scope.viewModel.del=function(department){
		$rootScope.alertConfirm("确定删除吗？",function() {
			$scope.viewModel.isLoading=false;
			Service.deleteDepartment({
				departmentId:department.departmentId
			},function(){
				$scope.viewModel.bindData();
				$scope.viewModel.isLoading=false;
				$rootScope.alertInfo("删除成功。");
			},function(error){
				$scope.viewModel.isLoading=false;
				$rootScope.alertError(error.fault.message);
			});
		});
	};
	
	/*   上级部门选择   */
	$scope.viewSelectUpDepartmentModel={};
	$scope.viewSelectUpDepartmentModel.perPage = "10";
	$scope.viewSelectUpDepartmentModel.rowCount = "1";
	//是否选中
	$scope.viewSelectUpDepartmentModel.isInAssignmentArr=function(department){
		if($scope.editModel.department.parentDepartmentId == department.departmentId){
			return true;
		}
		return false;
	};
	//加载数据
	$scope.viewSelectUpDepartmentModel.bindData = function(){
		$scope.viewSelectUpDepartmentModel.isLoading=true;
		//获取基本信息
		Service.getDepartmentList({
			pageNo:$scope.viewSelectUpDepartmentModel.rowCount,
			pageCount:$scope.viewSelectUpDepartmentModel.perPage,
			departmentName:$scope.viewSelectUpDepartmentModel.departmentName,
			corporationId:$scope.editModel.department.corporationId,
			fromDate:$scope.editModel.changeEffectiveDate,
		},
		function(data){
			console.log(data)
			$scope.viewSelectUpDepartmentModel.dataDepartmentList = data.departmentBeanList;
			$scope.viewSelectUpDepartmentModel.totalData = data.pageTotal;
			$scope.viewSelectUpDepartmentModel.isLoading=false;
		},function(error) {
			$scope.viewSelectUpDepartmentModel.isLoading=false;
		});
		
	};
	//加载
	$scope.viewSelectUpDepartmentModel.load=function(){
		$scope.viewSelectUpDepartmentModel.bindData();
		$scope.viewSelectUpDepartmentModel.show=true;
	};
	//关闭加载
	$scope.viewSelectUpDepartmentModel.unLoad=function(){
		$scope.viewSelectUpDepartmentModel.show=false;
	};
	// 上级部门选择按钮
	$scope.editModel.chooseUpde = function(){
		if($scope.editModel.department.corporationId == null){
			$rootScope.alertWarning("请先选公司，再选上级部门。");
			return;
		}
		$scope.viewSelectUpDepartmentModel.load();
		$scope.viewSelectUpDepartmentModel.title="上级部门选择";
	}
	$scope.editModel.chooseDelete = function(){
		$scope.editModel.department.parentDepartmentId = null;
		$scope.editModel.parentDepartment.upDepartment = null;
	}
	// 点击选择指定上级部门按钮
	$scope.viewSelectUpDepartmentModel.upDepartment = function(department){
		$scope.viewSelectUpDepartmentModel.show=false;
		$scope.viewModel.show=true;
		$scope.editModel.department.parentDepartmentCode=department.departmentCode;
		$scope.editModel.department.parentDepartmentName=department.departmentName;
		$scope.editModel.department.parentDepartmentId = department.departmentId;
	}
	//选择公司的方法
	$scope.viewModel.corporationChange = function(corporationId){
		$scope.editModel.department.corporationId = corporationId;
	}
	/*****公司选择*****/
	$scope.viewCorporationModel={};
	$scope.viewCorporationModel.dataList={};
	$scope.viewCorporationModel.corporation={};
	$scope.viewSelectCorporationModel={};
	$scope.viewSelectCorporationModel.show=false;
	$scope.viewCorporationModel.bindData = function(){
		$scope.viewCorporationModel.isLoading=true;
		//获取基本信息
		CorporationService.getCorporationList({
			fromDate:$scope.editModel.changeEffectiveDate,
		},
		function(data){
			console.log(data)
			$scope.viewCorporationModel.dataList = data;
			$scope.viewCorporationModel.isLoading=false;
		},function(error) {
			$scope.viewCorporationModel.isLoading=false;
		});
	};
	
	$scope.viewSelectCorporationModel.load=function(){
		$scope.viewSelectCorporationModel.bindCorporationData();
		$scope.viewSelectCorporationModel.show=true;
	};
	//关闭加载
	$scope.viewSelectCorporationModel.unLoad=function(){
		$scope.viewSelectCorporationModel.show=false;
	};
	// 上级部门选择按钮
	$scope.viewCorporationModel.chooseUpde = function(){
		$scope.viewSelectCorporationModel.show=true;
		$scope.viewCorporationModel.title="选择公司";
		$scope.viewCorporationModel.bindData();
	}
	$scope.viewCorporationModel.chooseDelete = function(){
		$scope.editModel.department.corporationId = null;
		$scope.editModel.department.corporationCode = null;
		$scope.editModel.department.corporationName = null;
	}
	// 点击选择指定上级部门按钮
	$scope.viewCorporationModel.upCorporation = function(corporation){
		$scope.viewSelectCorporationModel.show=false;
		$scope.viewModel.show=true;
		$scope.editModel.department.corporationId = corporation.corporationId;
		$scope.editModel.department.corporationCode = corporation.corporationCode;
		$scope.editModel.department.corporationName = corporation.corporationName;
	}
	$scope.viewCorporationModel.isInAssignmentArr=function(corporation){
		if($scope.editModel.department.corporationId == corporation.corporationId){
			return true;
		}
		return false;
	};
	$scope.viewModel.effectiveDateClose=function(){
    	if($scope.viewModel.radioValue==3){
    		
    	}else{
    		$scope.viewModel.effectiveDate=null;
    	}
    }

	//日期控件
	$scope.viewModel.dateOptions = {
			formatYear: 'yy',
		    startingDay: 1,
		    showWeeks:false
	};
	//日期控件
	$scope.viewModel.format = "yyyy/MM/dd";
	//日期启动开关
	$scope.viewModel.effectiveDateOpen = false;
	$scope.viewModel.effectiveDateOpnened = false;
	$scope.viewModel.effectiveDateOpen = function($event) {
		$event.preventDefault();
		$event.stopPropagation();
		$scope.viewModel.effectiveDateOpnened = !$scope.viewModel.effectiveDateOpnened;

		
	};
	//加载数据
	$scope.viewModel.bindData = function(changeEffectiveDate){
		if(changeEffectiveDate == 1){
			$scope.editModel.changeEffectiveDate = $scope.viewModel.effectiveDate;
		}
		$scope.viewCorporationModel.bindData();
		$scope.viewModel.isLoading=true;
		//获取基本信息
		Service.getDepartmentList({
			pageNo:$scope.viewModel.rowCount,
			pageCount:$scope.viewModel.perPage,
			departmentName:$scope.viewModel.departmentName,
			fromDate:$scope.viewModel.effectiveDate,
			corporationCodeList:$scope.viewModel.corporationList
		},
		function(data){
			console.log(data)
			$scope.viewModel.dataList = data.departmentBeanList;
			$scope.viewModel.totalData = data.pageTotal;
			$scope.viewModel.isLoading=false;
		},function(error) {
			$scope.viewModel.isLoading=false;
		});
		
	};
	
	$scope.viewModel.bindData();
} ]);